package com.tegistuff.webos.run;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.tegistuff.webos.WebOSAppListCellRenderer;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class WebOSRunConfigurationEditor extends SettingsEditor<WebOSRunConfiguration> {

	private DefaultComboBoxModel folderModel;
    private DefaultComboBoxModel packageModel;
    private DefaultComboBoxModel serviceModel;
    private DefaultComboBoxModel accountsModel;

	private JPanel contentPane;
	private JComboBox folderCombo;
	private JComboBox targetCombo;
	private JCheckBox loggingEnabled;
	private JCheckBox useV1;
    private JComboBox packageCombo;
    private JComboBox serviceCombo;
    private JComboBox accountsCombo;
    private JCheckBox useSeparateOutputFolder;
    private JSpinner debuggerPort;

    private void fillCombobox(JComboBox combo, List<VirtualFile> files, VirtualFile selected, boolean withNull) {
        DefaultComboBoxModel model = (DefaultComboBoxModel)combo.getModel();
        model.removeAllElements();
        if(withNull)
            model.addElement(null);
        for (VirtualFile file : files) {
            model.addElement(file);
        }
        if (selected == null && files.size() != 0)
            selected = files.get(0);
        combo.setSelectedItem(selected);
    }

    @Override
	protected void resetEditorFrom(WebOSRunConfiguration s) {
        fillCombobox(folderCombo, s.getValidAppFolders(), s.getAppFolder(), false);
        fillCombobox(packageCombo, s.getValidPackageFolders(), s.getPkgFolder(), true);
        fillCombobox(serviceCombo, s.getValidServiceFolders(), s.getSrvFolder(), true);
        fillCombobox(accountsCombo, s.getValidAccountsFolders(), s.getAccFolder(), true);
        debuggerPort.getModel().setValue(s.V8DebugPort);
		targetCombo.setSelectedItem(s.getTarget());
		useV1.setSelected(s.UseV1Format);
        useSeparateOutputFolder.setSelected(s.UseSeparateOutputFolders);
        loggingEnabled.setSelected(s.FollowLogs);
	}

	@Override
	protected void applyEditorTo(WebOSRunConfiguration s) throws ConfigurationException {
		s.setAppFolder((VirtualFile)folderCombo.getSelectedItem());
        s.setPkgFolder((VirtualFile)packageCombo.getSelectedItem());
        s.setSrvFolder((VirtualFile)serviceCombo.getSelectedItem());
        s.setAccFolder((VirtualFile)accountsCombo.getSelectedItem());
		s.setTarget((RunTarget) targetCombo.getSelectedItem());
        s.V8DebugPort = (Integer)debuggerPort.getModel().getValue();
		s.UseV1Format = useV1.isSelected();
        s.UseSeparateOutputFolders = useSeparateOutputFolder.isSelected();
        s.FollowLogs = loggingEnabled.isSelected();
	}

    private DefaultComboBoxModel prepareComboBox(JComboBox combo, ListCellRenderer renderer) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        combo.setModel(model);
        combo.setRenderer(renderer);
        return model;
    }

	@NotNull
	@Override
	protected JComponent createEditor() {
        folderModel = prepareComboBox(folderCombo, new WebOSAppListCellRenderer());
        packageModel = prepareComboBox(packageCombo, new WebOSAppListCellRenderer());
        serviceModel = prepareComboBox(serviceCombo, new WebOSAppListCellRenderer());
        accountsModel = prepareComboBox(accountsCombo, new WebOSAppListCellRenderer());
		targetCombo.setModel(new DefaultComboBoxModel(RunTarget.values()));
		return contentPane;
	}

	@Override
	protected void disposeEditor() {
	}

	{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
		$$$setupUI$$$();
	}

	/**
	 * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your code!
	 *
	 * @noinspection ALL
	 */
	private void $$$setupUI$$$() {
		contentPane = new JPanel();
		contentPane.setLayout(new GridLayoutManager(5, 3, new Insets(0, 0, 0, 0), -1, -1));
		final JLabel label1 = new JLabel();
		label1.setText("App:");
		contentPane.add(label1,
				new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED,
						GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		final Spacer spacer1 = new Spacer();
		contentPane.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
				GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
		final Spacer spacer2 = new Spacer();
		contentPane.add(spacer2, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_VERTICAL, 1,
				GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
		folderCombo = new JComboBox();
		contentPane.add(folderCombo, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
				GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		final JLabel label2 = new JLabel();
		label2.setText("Target:");
		contentPane.add(label2,
				new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED,
						GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		targetCombo = new JComboBox();
		contentPane.add(targetCombo, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL,
				GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		final JLabel label3 = new JLabel();
		label3.setText("Logging:");
		contentPane.add(label3,
				new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED,
						GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
		loggingEnabled = new JCheckBox();
		loggingEnabled.setText("Tail log file after launching");
		contentPane.add(loggingEnabled, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null,
				null, 0, false));
		useV1 = new JCheckBox();
		useV1.setText("Use V1 package format");
		contentPane.add(useV1, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null,
				null, 0, false));
		final JLabel label4 = new JLabel();
		label4.setText("Packaging:");
		contentPane.add(label4,
				new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED,
						GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
	}

	/**
	 * @noinspection ALL
	 */
	public JComponent $$$getRootComponent$$$() {
		return contentPane;
	}
}
